<?php
namespace Home\Controller;
use Think\Controller;
session_start();
class CustomerController extends Controller
{
	public function registered()
	{
		$this->display();
		
	}
    //注册
	public function zhuce()
	{
        $name=$_POST['name'];
		$phonenumber=$_POST['phonenumber'];
		$email=$_POST['email'];
		$password=($_POST['password']);
		$repassword=($_POST['repassword']);

		if (!empty($name)) {
		    $user=M('User');
		    //判断用户名是否已注册
			$select =$user ->where(['name'=>$name])->select();
			
			if ($select) {
				$this->redirect('login','',3,'该用户已经注册，请直接登录！...页面跳转中...');
			}
			$data=array(
                  'name'=>$name,
                  'phonenumber'=>$phonenumber,
                  'email'=>$email,
                  'password'=>md5($password),
                  'repassword'=>$repassword,
				);
			//写入数据库

				$insert=M('User')->add($data);
			    if ($insert) {
				    $this->redirect('Center/personal',array('name'=>$name),3,'注册成功！前往用户中心！...页面跳转中...');
			    }else{
				    $this->error('注册失败');
			}
			
		}
		
	}

	// public function login ()
	// {
	// 	$this->display();
	// }

   //登录
    public function denglu()
    {
    	//获取表单提交的数据
        $id=$_POST['id'];
    	$name=$_POST['name'];
    	$password=md5($_POST['password']);
    	$auto=$_POST['auto'];

    	$user = M('User');
    	$userinfo = $user->where('name="'.$name.'"')->find();
    	
    	

    	if (!empty($userinfo)) {
    		if ($userinfo['password']==$password) {
    			session('name',$userinfo['name']);
    			$this -> redirect('Center/personal',array('id'=>$id),1,'登录成功！前往用户中心！...页面跳转中...');
    		}else{
    			$this->error('密码错误');
    		}
    	}else{
    		$this->error('用户名不存在');
    	}
        
    	if ($userinfo['status']==0) {
            $this->error('该用户已被禁用!');
        }
    }


    public function login() {
        session('id', $user['id']);

        $user = M('user')->where(array('id' => session('id')))->find();
        $this->user = $user;
        $this->user_id = $user['id'];
        if (session('id')) {
            $this->redirect(U('Center/personal'));
        } else {
            $this->display();
        }
    }
    
    //判断用户有无登录
    // public function _initialize()
    // {
    //     $yong = isset($_SESSION['name']) ? $_SESSION['name']:null;
    //     if ($yong) {
            
    //         $this -> redirect('Center/personal',array('id'=>$id),3,'前往用户中心！...页面跳转中...');
    //     }else{
    //              $this->error('未登录，请登录',U('login'),3);
    //             }
    // }
    
    

    public function head()
    {
        $this->display();
    }

    public function tail()
    {
        $this->display();
    }

    
}